import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/models/public_msg_manger.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/utils/aha_utils.dart';
import 'package:jetour_line/utils/extension_int.dart';

import '../../aha_manger.dart';
import '../../network/aha_http.dart';
import '../../network/jetour_http.dart';
typedef _RefCallBack = void Function();
class BottomWidget extends StatefulWidget {
  final int type; //1 路书 2 共创 3轨迹
  final int typeId;
  final bool fav;
  final String shareTitle;
  final String shareSubTitle;
  final String shareImgUrl;
  final _RefCallBack callBack;
  Function? collectCallBack;
  BottomWidget(
      {Key? key,
      required this.type,
      required this.typeId,
      required this.shareTitle,
      required this.shareSubTitle,
        this.collectCallBack,
      required this.shareImgUrl, required this.fav,required this.callBack})
      : super(key: key);

  @override
  State<BottomWidget> createState() => _BottomWidgetState();
}

class _BottomWidgetState extends State<BottomWidget> {
  bool isCollect = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    isCollect = widget.fav;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 100.px,
      color: MyColors.white,
      child: Row(
        children: [for (int i = 0; i < 2; i++) Expanded(child: itemWidget(i))],
      ),
    );
  }

  Widget itemWidget(int index) {

    String img = '';
    String title = '';

    if (index == 0) {
      img = AhaUtils.getImagePath(isCollect ? 'ls_tab_colsle' : 'ls_tab_col');
      print("img === $img");
      title = isCollect?"已收藏":'收藏';
    } else if (index == 1) {
      img = AhaUtils.getImagePath('icon_share');
      title = '分享';
    }

    return Row(
      children: [
        Expanded(
          child: GestureDetector(
            onTap: () {
              if (index == 0) {
                collect();
              } else {
                String url;
                if (widget.type == 1) {//1 路书 2 共创 3轨迹
                  url =
                      'https://roadbook.jetour.com.cn/#/pages/line/details?id=${widget.typeId}';
                }else if (widget.type == 2)   {//1 路书 2 共创 3轨迹
                  url =
                      'https://roadbook.jetour.com.cn/#/pages/jetour-line/details?id=${widget.typeId}';
                }else if (widget.type == 3)   {//1 路书 2 共创 3轨迹
                  url =
                  'https://roadbook.jetour.com.cn/#/pages/track/details?id=${widget.typeId}';
                }else{
                  return;
                }
                AhaManger ahaManger = AhaManger();
                if (ahaManger.shareCallBack != null) {
                  ahaManger.shareCallBack!(0, widget.shareTitle,
                      widget.shareSubTitle, widget.shareImgUrl, url);
                }
              }
            },
            child: Container(
              height: 100.px,
              color: Colors.transparent,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Image(
                    image: AssetImage(img),
                    width: 48.px,
                    height: 46.px,
                  ),
                  Text(
                    title,
                    style:
                        TextStyle(fontSize: 28.px, color: MyColors.color898989),
                  )
                ],
              ),
            ),
          ),
        ),
        if (index != 3)
          Container(
            width: 1,
            height: 23.px,
            color: MyColors.colorE1E1E1,
          )
      ],
    );
  }

  void collect() async {
    if(PublicMsgManger().userInfoModel == null){
      if(AhaManger().loginCallBack!= null){
        AhaManger().loginCallBack!((String token) {
          if (token.isNotEmpty) {
            widget.callBack();
          }
        });
      }
      return;
    }
    if(widget.type == 1 || widget.type == 3){ //路书详情收藏  轨迹收藏
      Map<String, dynamic> params = Map();
      params['id'] = widget.typeId;
      if(widget.type == 1){
        params['type'] = 1;
      }else{
        params['type'] = 12;
      }
      AhaHttp().post(AhaRouter.MyCollectOne, data: params).then((value) {
        int fav = value['fav'];
        isCollect =  fav == 1;
        setState(() {
        });
      });
    }else{////共创详情收藏
      JetourHttp().post(JetourRouter.CommonCollect,
          data: {"id": widget.typeId}).then((result) {
        isCollect = result['collect'];
        if(widget.collectCallBack != null){
          widget.collectCallBack!(isCollect);
        }
        setState(() {

        });
      }).catchError((e) {});
    }

  }
}
